package fm.last.api
{
    import fm.last.BaseTC;
    

    public class AlbumAPITest extends BaseTC
    {
                
        public function AlbumAPITest()
        {
            super();            
            addTest("testAddTags");
            addTest("testGetInfo");
            addTest("testGetTags"); 
            addTest("testRemoveTag");
            addTest("testSearch");                  
        }

        public function testAddTags():void
        {      
            var getThenRemoveTag:Function = function(xml:XML):void {
                
                var f:Function = expectedPattern("/lfm/tags",/vp[12]/);
                f(xml);
                
                f = expectedPattern("/lfm/tags",/vp3/);
                try {
                    f(xml);    
                    fail("undesired tag exists!");
                } catch (e:Error) {}
                
                lfm_service.album.removeTag(okNoContent, unexpectedError, "Björk", "Post", "vp1");
                lfm_service.album.removeTag(okNoContent, unexpectedError, "Björk", "Post", "vp2");
            
            };
            
            var addThenGetTag:Function = function(xml:XML):void {
                var f:Function = okNoContent;
                f(xml);
                                
                lfm_service.album.getTags(getThenRemoveTag, unexpectedError, "Björk", "Post");                
            };
            
            lfm_service.album.addTags(addThenGetTag, unexpectedError, "Björk", "Post", "vp1,vp2");
                 
            lfm_service.album.addTags(unexpectedComplete, expectedError, "Björk", "I_Bet_No_Such_Album", "test1");                
        }       
        
        public function testGetInfo():void
        {      
            // if both (artist and album) are defined, ignore (mbid) can be ignored.
            lfm_service.album.getInfo(expectedPattern("/lfm/album/name",/Post/), unexpectedError, "Björk", "Post", null);            
            lfm_service.album.getInfo(expectedPattern("/lfm/album/artist",/Björk/), unexpectedError, "Björk", "Post", "aaaaaaaaaa-bbbb-cccc-aaaa-0f8e60062a68");
            lfm_service.album.getInfo(unexpectedComplete, expectedError, "Björk", null, null);
            lfm_service.album.getInfo(unexpectedComplete, expectedError, null, "Post", null);         
                        
            // if at leadt one of (artist and album) is not defined, (mbid) is used for retrieval. 
            // (artist and album) is ignored.   
            var mbid:RegExp = /095b8e2c-b4b9-45e7-b393-0f8e60062a68/;                                         
            lfm_service.album.getInfo(expectedPattern("/lfm/album/mbid", mbid), unexpectedError, null, null, "095b8e2c-b4b9-45e7-b393-0f8e60062a68");                        
            lfm_service.album.getInfo(expectedPattern("/lfm/album/mbid", mbid), unexpectedError, "Björk", null, "095b8e2c-b4b9-45e7-b393-0f8e60062a68");
            lfm_service.album.getInfo(expectedPattern("/lfm/album/mbid", mbid), unexpectedError, null, "Post", "095b8e2c-b4b9-45e7-b393-0f8e60062a68");            
            lfm_service.album.getInfo(expectedPattern("/lfm/album/mbid", mbid), unexpectedError, "Suede", null, "095b8e2c-b4b9-45e7-b393-0f8e60062a68");
            lfm_service.album.getInfo(expectedPattern("/lfm/album/mbid", mbid), unexpectedError, null, "Amnesiac", "095b8e2c-b4b9-45e7-b393-0f8e60062a68");

            // (artist + album) must == (mbid) 
            lfm_service.album.getInfo(unexpectedComplete, expectedError, "Björk", "I_Bet_No_Such_Album", "095b8e2c-b4b9-45e7-b393-0f8e60062a68");
            lfm_service.album.getInfo(expectedPattern("/lfm/album/mbid", mbid), unexpectedError, "Björk", "Post", "095b8e2c-b4b9-45e7-b393-0f8e60062a68");
            
            lfm_service.album.getInfo(unexpectedComplete, expectedError, "Björk", "I_Bet_No_Such_Album", null);                
        }
        
        /** @see testAddTags*/
        public function testGetTags():void
        {                                           
        }
        
        /** @see testAddTags*/
        public function testRemoveTag():void
        {      
            // tag must exist.
            lfm_service.album.removeTag(unexpectedComplete, expectedError, "Björk", "Post", "notexistvptag");                 
            lfm_service.album.removeTag(unexpectedComplete, expectedError, "Björk", "I_Bet_No_Such_Album", "vp1");       
        }
        
        public function testSearch():void
        {      
            lfm_service.album.search(expectedPattern("/lfm/results/albummatches", /(\S)/), 
                                    unexpectedError, "Post");
                                                                 
            lfm_service.album.search(expectedPattern("/lfm/results/albummatches", /^$/), 
                                     unexpectedError, "I_Bet_No_Such_Album");                
        }
    }
}